<template>
  <div class="">
    <quill-editor
      class="editor"
      v-model="content"
      ref="myQuillEditor"
      :options="editorOption"
      @blur="onEditorBlur($event)"
      @focus="onEditorFocus($event)"
      @change="onEditorChange($event)"
      @ready="onEditorReady($event)"
    >
    </quill-editor>
    <el-upload
      v-show="false"
      class="upload"
      :action="$http + '/cos/tencent/upload'"
      list-type="picture-card"
      :on-success="onSuccess"
      accept=".jpg,.png"
    >
    </el-upload>
  </div>
</template> 

<script>
import ImageResize from "quill-image-resize-module"; // 引用，调整图片大小
import { quillEditor, Quill } from "vue-quill-editor";

import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

//自定义样式一定要在原插件css下面引入
import "quill/assets/css/quillEditor.css";
// 自定义字体大小
var sizes = ["14px", "16px", "18px", "20px", "22px", "26px", "28px", "30px"];
var Size = Quill.import("formats/size");
Size.whitelist = sizes;
Quill.register("modules/imageResize", ImageResize);
// 自定义字体
var fonts = [
  "Microsoft-YaHei",
  "SimHei",
  "SimSun",
  "KaiTi",
  "FangSong",
  "Arial",
  "sans-serif",
];
var Font = Quill.import("formats/font");
Font.whitelist = fonts;
Quill.register(Font, true);

export default {
  name: "",
  props: {
    contenta: {
      type: String,
    },
  },
  components: { quillEditor },
  data() {
    return {
      // 富文本编辑器配置
      editorOption: {
        modules: {
          toolbar: {
            container: [
              ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
              ["blockquote", "code-block"], // 引用  代码块
              [{ header: 1 }, { header: 2 }], // 1、2 级标题
              [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
              [{ script: "sub" }, { script: "super" }], // 上标/下标
              [{ indent: "-1" }, { indent: "+1" }], // 缩进
              [{ direction: "rtl" }], // 文本方向
              [{ size: sizes }], // 字体大小
              [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
              [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
              [{ font: fonts }], // 字体
              [{ align: [] }], // 对齐方式
              ["clean"], // 清除文本格式
              ["image", "video"], // 链接、图片、视频
            ],
            handlers: {
              image(value) {
                if (value) {
                  // value === true
                  document.querySelector(".upload input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
          imageResize: {
            displayStyles: {
              backgroundColor: "black",
              border: "none",
              color: "white",
            },
            modules: ["Resize", "DisplaySize", "Toolbar"],
          },
        },
        placeholder: "请输入正文",
      },
      content: "",
    };
  },
  methods: {
    // 失去焦点事件
    onEditorBlur(quill) {
      // console.log("editor blur!", quill);
    },
    // 获得焦点事件
    onEditorFocus(quill) {
      // console.log("editor focus!", quill);
    },
    // 准备富文本编辑器
    onEditorReady(quill) {
      // console.log("editor ready!", quill, 888);
    },
    // 内容改变事件
    onEditorChange({ quill, html, text }) {
      console.log("editor change!", quill, html, text);
      this.content = html;
      // console.log(html);
      this.$emit("editorChange", html);
    },
    onSuccess(e) {
      //图片上传成功
      console.log(e);
      const imageUrl = e.data;

      // 获取光标所在位置
      let quill = this.$refs.myQuillEditor.quill;
      let length = quill.getSelection().index;
      // 插入图片
      quill.insertEmbed(length, "image", imageUrl);
      // 调整光标到最后
      quill.setSelection(length + 1);
      this.content += imageUrl;
    },
  },

  created() {},

  mounted() {},

  computed: {},

  watch: {
    contenta: {
      handler(newName, oldName) {
        // console.log(newName, 333);
        this.content = newName;
      },
      immediate: true,
    },
  },
};
</script>

<style lang='scss' scoped>
.editor {
  height: 300px;
}
</style>